/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.mapper.JsonMapper;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.zhixun.mobile.common.utils.StringUtils;
import com.boxin.ims.modules.wechat.entity.JiuWechatMember;
import com.boxin.ims.modules.wechat.entity.LangType;
import com.boxin.ims.modules.wechat.entity.WeChat;
import com.boxin.ims.modules.wechat.entity.WechatMember;
import com.boxin.ims.modules.wechat.service.JiuWechatMemberService;
import com.boxin.ims.modules.wechat.service.WeChatService;
import com.boxin.ims.modules.wechat.utils.TokenUtils;
import com.boxin.ims.modules.wechat.utils.WechatInterfaceUtils;

/**
 * 微信会员Controller
 * @author sxh
 * @version 2018-08-15
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/wechat/jiuWechatMember")
public class JiuWechatMemberController extends BaseController {

	@Autowired
	private JiuWechatMemberService jiuWechatMemberService;
	@Autowired
	private WeChatService weChatService;
	@ModelAttribute
	public JiuWechatMember get(@RequestParam(required=false) Long id) {
		if (id != null){
			return jiuWechatMemberService.get(id);
		}else{
			return new JiuWechatMember();
		}
	}
	
	@RequiresPermissions("wechat:jiuWechatMember:view")
	@RequestMapping(value = {"list", ""})
	public String list(JiuWechatMember jiuWechatMember, HttpServletRequest request, HttpServletResponse response, Model model) {
		if(jiuWechatMember.getUser()==null){
			jiuWechatMember.setUser(null);
		}
		Page<JiuWechatMember> page = jiuWechatMemberService.find(new Page<JiuWechatMember>(request, response), jiuWechatMember); 
        model.addAttribute("page", page);
		return "modules/wechat/jiuWechatMemberList";
	}

	@RequiresPermissions("wechat:jiuWechatMember:view")
	@RequestMapping(value = "form")
	public String form(JiuWechatMember jiuWechatMember, Model model) {
	   
		model.addAttribute("jiuWechatMember", jiuWechatMember);
		return "modules/wechat/jiuWechatMemberForm";
	}

	@RequiresPermissions("wechat:jiuWechatMember:edit")
	@RequestMapping(value = "save")
	public String save(JiuWechatMember jiuWechatMember, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, jiuWechatMember)){
			return form(jiuWechatMember, model);
		}
		jiuWechatMemberService.save(jiuWechatMember);
		addMessage(redirectAttributes, "保存微信会员'" + jiuWechatMember.getName() + "'成功");
		return "redirect:"+Global.ADMIN_PATH+"/wechat/jiuWechatMember/?repage";
	}
	
	@RequiresPermissions("wechat:jiuWechatMember:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		jiuWechatMemberService.delete(id);
		addMessage(redirectAttributes, "删除微信会员成功");
		return "redirect:"+Global.ADMIN_PATH+"/wechat/jiuWechatMember/?repage";
	}
	
	/***
	 * 
	 * @function: <p> 同步会员信息</p>
	 * @return
	 */
	@RequestMapping(value = "syncFromWechatMember")
	public String syncFromWechatMember(HttpServletRequest request,
			Model model,RedirectAttributes redirectAttributes) throws Exception {
		String id = request.getParameter("id"); 
		Map<String,Object> map=new HashMap<String,Object>();
		if(StringUtils.isNotEmpty(id)){
			WeChat wechat = weChatService.getWeChatByUserId(2l);
			if(wechat!=null){
				String accessToken = TokenUtils.getAPIToken(wechat);
				JiuWechatMember wechatMember = jiuWechatMemberService
						.get(Long.parseLong(id));
				
				if (wechatMember != null ) {
						try {
							JiuWechatMember temp = WechatInterfaceUtils
									.getJiuUserMessage(accessToken,
											wechatMember.getOpenId(),
											LangType.zh_CN);
							if(StringUtils.isNotEmpty(temp.getNickname())){
								wechatMember.setNickname(temp.getNickname());
							}
							wechatMember.setAge(temp.getAge());
							wechatMember.setProvince(temp.getProvince());
							wechatMember.setCity(temp.getCity());
							wechatMember.setCountry(temp.getCountry());
							wechatMember.setLanguage(temp.getLanguage());
							wechatMember.setHeadimgurl(temp.getHeadimgurl());
							jiuWechatMemberService.save(wechatMember);
							addMessage(redirectAttributes, "同步会员【"+wechatMember.getNickname()+"】信息成功!"); 
						} catch (Exception e) {
							e.printStackTrace();
						}
				}
			}
					
		}
		return "redirect:"+Global.ADMIN_PATH+"/wechat/jiuWechatMember/?repage";		
	}

}
